#include "stdafx.h"
#include "CharClass.h"

void CharClassList::DeleteChar(WCHAR Temp)
{
	for(int i=0;i<CharList.Size();i++)
		if(CharList[i]->Start == Temp)
			CharList.Delete(i);
}

int CharClassList::AddChar(WCHAR Temp,BOOL bNot)
{
	NAutoPtr<CharClass> Class = new CharClass;
	Class->Start = Class->End = Temp;
	if(bNot) Class->Type = gtCharNot;
	else Class->Type = gtChar;
	Class->Index = CharList.Size();
	if(InList(Class)) return -1;
	CharList.Add(Class);
	return CharList.Size() - 1;
}

int CharClassList::AddChars(WCHAR Temp1,WCHAR Temp2,BOOL bNot)
{
	NAutoPtr<CharClass> Class = new CharClass;
	Class->Start = Temp1;
	Class->End = Temp2;
	if(bNot) Class->Type = gtCharGroupNot;
	else Class->Type = gtCharGroup;
	Class->Index = CharList.Size();
	if(InList(Class)) return -1;
	CharList.Add(Class);
	return CharList.Size() - 1;
}

int CharClassList::AddOperator(WCHAR Temp)
{
	NAutoPtr<CharClass> Class = new CharClass;
	Class->Start = Class->End = Temp;
	Class->Type = gtOperator;
	Class->Index = CharList.Size();
	if(InList(Class)) return -1;
	CharList.Add(Class);
	return CharList.Size() - 1;
}

int CharClassList::AddTransferred(WCHAR Temp)
{
	NAutoPtr<CharClass> Class = new CharClass;
	Class->Start = Class->End = Temp;
	Class->Type = gtTransferred;
	Class->Index = CharList.Size();
	if(InList(Class)) return -1;
	CharList.Add(Class);
	return CharList.Size() - 1;
}

NAutoPtr<CharClass> CharClassList::GetChar(WCHAR Temp,BOOL bNot)
{
	if(!bNot)
	{
		for(int i=0;i<CharList.Size();i++)
			if(CharList[i]->Type == gtChar && Temp == CharList[i]->Start)
				return CharList[i];
	}
	else
	{
		for(int i=0;i<CharList.Size();i++)
			if(CharList[i]->Type == gtCharNot && Temp == CharList[i]->Start)
				return CharList[i];
	}
	return 0;
}

NAutoPtr<CharClass> CharClassList::GetChars(WCHAR Temp1,WCHAR Temp2,BOOL bNot)
{
	if(!bNot)
	{
		for(int i=0;i<CharList.Size();i++)
			if(CharList[i]->Type == gtCharGroup && Temp1 == CharList[i]->Start && Temp2 == CharList[i]->End)
				return CharList[i];
	}
	else
	{
		for(int i=0;i<CharList.Size();i++)
			if(CharList[i]->Type == gtCharGroupNot && Temp1 == CharList[i]->Start && Temp2 == CharList[i]->End)
				return CharList[i];
	}
	return 0;
}

NAutoPtr<CharClass> CharClassList::GetOperator(WCHAR Temp)
{
	for(int i=0;i<CharList.Size();i++)
		if(CharList[i]->Type == gtOperator && Temp == CharList[i]->Start)
			return CharList[i];
	return 0;
}

NAutoPtr<CharClass> CharClassList::GetTransferred(WCHAR Temp)
{
	for(int i=0;i<CharList.Size();i++)
		if(CharList[i]->Type == gtTransferred && Temp == CharList[i]->Start)
			return CharList[i];
	return 0;
}

BOOL CharClassList::InList(NAutoPtr<CharClass> Temp)
{
	for(int i=0;i<CharList.Size();i++)
		if(*Temp.Buffer() == *CharList[i].Buffer())
			return TRUE;
	return FALSE;
}

NWString CharClassList::Print()
{
	NWString Result,str;
	Result.Format(L"CharClassList:\n");
	for(int i=0;i<CharList.Size();i++)
		if(CharList[i]->Type == gtCharGroup)
		{
			str.Format(L"%c - %c\t\t%d\n",CharList[i]->Start,CharList[i]->End,i);
			Result += str;
		}
		else if(CharList[i]->Type == gtCharGroupNot)
		{
			str.Format(L"%c - %c(Not)\t\t%d\n",CharList[i]->Start,CharList[i]->End,i);
			Result += str;
		}
		else if(CharList[i]->Type == gtCharNot)
		{
			str.Format(L"%c(Not)\t\t%d\n",CharList[i]->Start,i);
			Result += str;
		}
		else
		{
			str.Format(L"%c\t\t%d\n",CharList[i]->Start,i);
			Result += str;
		}
	wprintf(Result.GetBuffer());
	return Result;
}